import "reflect-metadata"; //作为装饰器的基础 写在前面
import { InversifyExpressServer } from "inversify-express-utils";
import { Container } from "inversify";
import { User } from "./src/user/Controller";
import { UserService } from "./src/user/service";
import { Post } from "./src/post/Controller";
import { PostService } from "./src/post/service";
import { PrismaClient } from "@prisma/client";
import express from "express";
import { PrismaDB } from "./src/db";
import { JWT } from "./src/jwt";
const container = new Container();
/**
 * user 模块 注入到容器中，就可以在construct中相互调用了
 */
container.bind(User).to(User);
container.bind(UserService).to(UserService);
/**
 * post 模块 注入到容器中，就可以在construct中相互调用了
 */
container.bind(Post).to(Post);
container.bind(PostService).to(PostService);
/**
 * 封装prismaClient
 */
container.bind<PrismaClient>("PrismaClient").toFactory(() => {
  return () => {
    return new PrismaClient();
  };
});
/**
 * PrismaDB
 */
container.bind(PrismaDB).to(PrismaDB);
/**
 * 注入JWT
 */
container.bind(JWT).to(JWT);
const server = new InversifyExpressServer(container);
// 因为接收不到post请求，所以需要使用中间件
server.setConfig((app) => {
  app.use(express.json()); // for parsing application/json
  app.use(container.get(JWT).init()); // 关联express
});
const app = server.build();
app.listen(3001, () => {
  console.log("Server started on port 3001");
});
